package com.google.android.mypetrol;

import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

public class HistoryActivity extends Activity {
	
	private HistoryList mHistoryList;
	
	private ContentResolver mResolver;
	private Resources mResources;
	
	private String mSelection = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mResolver = getContentResolver();
        mResources = getResources();
        
        mHistoryList = new HistoryList(this, getCursor());
        setContentView(mHistoryList);
        mHistoryList.setListCallBack(mOnItemClickListener);
        mHistoryList.setFilterClickListener(mFilterListener);
    }

	private View.OnClickListener mFilterListener = new View.OnClickListener () {
		public void onClick(View v) {
			int year = mHistoryList.getFilterYear();
			int month = mHistoryList.getFilterMonth();

			if (year != 0){
				mSelection = "year="+ year;
				if (month != 0)
					mSelection += " and month=" + month;
			}else {
				mSelection = null ;
			}
			Log.d("shit", "selection = " + mSelection);
			mHistoryList.updateCursor(getCursor());
		}
	};
    
    private AdapterView.OnItemClickListener mOnItemClickListener  = 
    	new AdapterView.OnItemClickListener() {
    	
    	@SuppressWarnings("unchecked")
    	public void onItemClick(AdapterView arg0, View arg1, int position, long id) {
    		Intent intent = new Intent(HistoryActivity.this, PetrolDetailActivity.class);
    		intent.putExtra(MyPetrol.ITEM_TYPE, MyPetrol.MODE_MODIFY);
    		intent.putExtra(MyPetrol.ITEM_INDEX, position);
			startActivityForResult(intent, 0);
    	}
    };

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		mHistoryList.updateListCursor();
	}
	
	private Cursor getCursor() {
		return mResolver.query(Provider.CONTENT_URI, null, mSelection,
				null, "datetime");
	}
	
	private void deleteAllHistory() {
		Log.d("shit", "delete all");
		Cursor c = mHistoryList.getCursor();
		while (c.getCount() != 0)
			c.deleteRow();
	}
	
	private void deleteSelectedHistory() {
		Log.d("shit", "delete selected");
		Cursor c = mHistoryList.getCursor();
		List<Integer> list = mHistoryList.getSelectedItem();
		int count = mHistoryList.getSelectedItem().size();
		for(int i = count - 1; i >= 0; i -- ) {
			c.moveToPosition(list.get(i));
			c.deleteRow();
		}
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch( item.getItemId()) {		
		case 0:
			deleteSelectedHistory();
			break;
		case 1:
			deleteAllHistory();
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.hitstory_menu, menu);

	    return super.onCreateOptionsMenu(menu);
	}
	
	
}